# Copyright (C) 2010 Razvan Constantin <razvan.constantin@rconst.net>
#
# This file is part of FileBow.
#
# FileBow is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FileBow is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FileBow.  If not, see <http:#www.gnu.org/licenses/>.

# WAF task extensions for running a cxx program after link. 

import TaskGen
import Task
import Logs

@TaskGen.taskgen
@TaskGen.feature('crun')
@TaskGen.after('apply_link')
def crun(self):
  """ Executes a cprogram target after it has been built. """
  if not 'cprogram' in self.features:
    Logs.error('Only cprogram can be executed %s' % self)
    return
  task = cxx_run(self.env.copy(), self)
  task.set_inputs(self.link_task.outputs)

class cxx_run(Task.Task):
  def __init__(self, env, generator):
    Task.Task.__init__(self, env, generator=generator)

  def run(self):
    for input in self.inputs:
      cmd = input.abspath(self.env)
      cmd_dir = input.parent.abspath(self.env)
      Logs.debug('Running %s in %s' % (cmd, cmd_dir))
      self.exec_command(cmd, cwd = cmd_dir)
    return 0

  quiet = 1
  after = [ 'cxx_link', 'cc_link' ]

Task.always_run(cxx_run)
